import { Vec2 } from "cc";

export interface ITouchCtrl {
    onBind?(): void;
    onUnBind?(): void;
    onTouch?(wp: Vec2): void;
    onMove?(wp: Vec2, delta: Vec2): void;
    onEnd?(wp: Vec2): void;
    block(): void;
    unblock(): void;
}
export default class Toucher {
    static touchCtrl: ITouchCtrl = null;
    static bindToucher(touchCtrl: ITouchCtrl) {
        this.touchCtrl && this.touchCtrl.onUnBind && this.touchCtrl.onUnBind();
        this.touchCtrl = touchCtrl;
        this.touchCtrl && this.touchCtrl.onBind && this.touchCtrl.onBind();
    }
    static unbindToucher(touchCtrl: ITouchCtrl) {
        if (this.touchCtrl == touchCtrl)
            this.touchCtrl = null;
    }
    private static isBlock = false;
    public static block() {
        this.isBlock = true;
    }
    public static unblock() {
        this.isBlock = false;
    }
    public static blockCtrl() {
        this.touchCtrl?.block();
    }
    public static unblockCtrl() {
        this.touchCtrl?.unblock();
    }
    public static onTouch(wp: Vec2) {
        if (this.isBlock) {
            console.warn("block toucher");
            return;
        }
        if (!this.touchCtrl) {
            console.warn("没有绑定toucher");
        }
        this.touchCtrl?.onTouch(wp);
    }
    public static onMove(wp: Vec2, delta: Vec2) {
        if (this.isBlock) {
            console.warn("block toucher");
            return;
        }
        if (!this.touchCtrl) {
            console.warn("没有绑定toucher");
        }
        this.touchCtrl?.onMove(wp, delta);
    }
    public static onEnd(wp: Vec2) {
        if (!this.touchCtrl) {
            console.warn("没有绑定toucher");
        }
        this.touchCtrl?.onEnd(wp);
    }

}